<template>
	<view class="addText">
		<HeaderBar showBack fixed backgroundColor="#F6F6F6">
			<template #title>
				<view class="htitle">
					<view class="text">
						地址本
					</view>
					<view class="selectChain" @click="open">
						<text>{{selctChainName}}</text>
						<image src="../../static/images/personal/down.png" style="width: 21upx;" class="icon"
							mode="widthFix"></image>
					</view>
				</view>
			</template>
			<template #right>
				<image src="/static/images/personal/add.png" mode="widthFix" style="width: 38upx;" @tap="addAddress">
				</image>
			</template>
		</HeaderBar>

		<view class="contentBox">
			<view class="list" v-if="list.length>0">
				<view class="item" v-for="(item,index) in list" :key="index" @tap="editAddress(item)">
					<image :src="'/static/images/chain/' +item.chainName+'.png'" mode="" class="chainIcon"></image>
					<view class="con">
						<view class="name">
							{{item.addressName}}
						</view>
						<view class="address">
							{{$utils.formatAddress(item.address)}}
						</view>
					</view>
					<image src="../../static/images/personal/iptRight.png" mode="widthFix" class="icon"></image>
				</view>
			</view>
			<view class="empty" v-if="list.length==0">
				<image src="/static/images/personal/empty.png" class="icon" mode="widthFix"></image>
				<view class="text">
					您当前没有添加地址信息
				</view>
				<view class="btn" @tap="addAddress">
					点击添加
				</view>
			</view>
		</view>

		<uni-popup ref="popup" type="bottom" background-color="#FFFFFF" border-radius="10px 10px 0 0">
			<view class="popupTitle">
				请选择网络
				<text class="iconfont icon-guanbi"></text>
			</view>
			<scroll-view scroll-y="true" class="scroll-Y">
				<view class="">
					<view class="scroll-view-item" @tap="selectChain()">
						<image src="/static/images/personal/all.png" mode="widthFix" class="icon"></image>
						<view class="chain">
							<view class="name">
								所有网络
							</view>
							<view class="checkbox">
								<image src="../../static/images/personal/iptRight.png" mode="widthFix" class="icon">
								</image>
							</view>
						</view>
					</view>
				</view>
				<view class="" v-for="(item,index) in allchains" :key="index">
					<view class="scroll-view-item" @tap="selectChain(item)">
						<image :src="'/static/images/chain/' +item.chainName+'.png'" mode="widthFix" class="icon"></image>
						<view class="chain">
							<view class="name">
								{{item.chainName}}
							</view>
							<view class="checkbox">
								<image src="../../static/images/personal/iptRight.png" mode="widthFix" class="icon">
								</image>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				selctChainID: null,
				selctChainName:"所有网络",
			}
		},
		computed: {
			allchains() {
				return this.$store.getters.getAllchains;
			},
			chainId(){
				return this.$store.getters.getChainId;
			}
		},
		mounted() {
			this.getList()
			uni.$on("addressBook", ()=>{
				if(this.selctChainID){
					this.getIdList()
				}else{
					this.getList()
				}
			})
		},
		methods: {
			open() {
				this.$refs.popup.open('bottom')
			},
			editAddress(item) {
				const info = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({
					url: `/pages/editAddress/editAddress?info=${info}`
				})
			},
			addAddress() {
				uni.navigateTo({
					url: `/pages/addAddress/addAddress?selctChainID=${this.selctChainID||this.chainId}`
				})
			},
			selectChain(item){
				this.$refs.popup.close()
				if(item){
					this.selctChainID = item.chainId+""
					this.selctChainName = item.chainName
					this.getIdList()
				}else{
					this.selctChainID = null
					this.selctChainName = "所有网络"
					this.getList()
				}
			},
			getIdList() {
				uni.$db.select('list', { // 普通查询
					from: 'addressBook',
					where: [{
						chainId: `= "${this.selctChainID}"`,
					}],
				}).then(res => {
					if (res.length > 0) {
						this.list = res.reverse()
					}else{
						this.list =[]
					}
				})
			},
			getList() {
				uni.$db.select('list', { // 普通查询
					from: 'addressBook',
					where: []
				}).then(res => {
					if (res.length > 0) {
						this.list = res.reverse()
					}else{
						this.list =[]
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.addText {
		min-height: 100%;
		background: #F6F6F6;
		font-size: 26upx;
		display: flex;
		flex-direction: column;
		justify-content: center;

		.htitle {
			.text {
				font-size: 30upx;
				color: #3D3D3D;
				text-align: center;
			}

			.selectChain {
				height: 32upx;
				display: flex;
				align-items: center;
				background: #ECF4FF;
				color: #2D7C18;
				font-size: 18upx;
				border: 1upx solid #5B7B8A;
				padding: 0 20upx;
				border-radius: 8upx;

				.icon {
					margin-left: 4upx;
				}
			}
		}

		.contentBox {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			padding: 20upx 0;

			.list {
				flex: 1;
				padding: 0 30upx;

				.item {
					display: flex;
					align-items: center;
					background: #FFF;
					padding: 20upx 30upx;
					margin-top: 20upx;
					border-radius: 10upx;

					.chainIcon {
						width: 68upx;
						height: 68upx;
					}

					.icon {
						width: 20upx;
						height: 20upx;
					}

					.con {
						flex: 1;
						margin-left: 20upx;
						.address{
							color: #A7A7A7;
						}
					}
				}
			}

			.empty {
				display: flex;
				flex-direction: column;
				align-items: center;

				.icon {
					width: 130upx;
					height: 146upx;
				}

				.text {
					color: #B2B2B2;
					margin-top: 32upx;
				}

				.btn {
					color: #2D7C18;
					border: 2upx solid #2D7C18;
					border-radius: 10upx;
					width: 274upx;
					height: 76upx;
					text-align: center;
					line-height: 76upx;
					margin-top: 52upx;
				}
			}

		}

		.scroll-Y {
			height: 50vh;

			.scroll-view-item {
				height: 120upx;
				display: flex;
				align-items: center;
				padding: 0 30upx;

				.icon {
					width: 68upx;
					height: 68upx;
				}

				.chain {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-left: 30upx;
					font-size: 28upx;
					border-bottom: 0.5px solid #D8D8D8;
					height: 100%;

					.checkbox {
						display: flex;
						.icon {
							width: 20upx;
							height: 20upx;
						}
					}

				}
			}
		}

		.popupTitle {
			text-align: center;
			position: relative;
			height: 80upx;
			line-height: 80upx;
			font-size: 28upx;

			.iconfont {
				position: absolute;
				right: 30upx;
			}
		}
	}
</style>